home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / init.d / alsa-utils < prev    next >
Encoding:
Text File  |  2013-01-10  |  9.6 KB  |  385 lines

  1. #!/bin/sh
  2. #
  3. # alsa-utils initscript
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides:          alsa-utils
  7. # Required-Start:    $remote_fs udev
  8. # Required-Stop:     $remote_fs
  9. # Default-Start:     S
  10. # Default-Stop:
  11. # Short-Description: Restore and store ALSA driver settings
  12. # Description:       This script stores and restores mixer levels on
  13. #                    shutdown and bootup.On sysv-rc systems: to
  14. #                    disable storing of mixer levels on shutdown,
  15. #                    remove /etc/rc[06].d/K50alsa-utils.  To disable
  16. #                    restoring of mixer levels on bootup, rename the
  17. #                    "S50alsa-utils" symbolic link in /etc/rcS.d/ to
  18. #                    "K50alsa-utils".
  19. ### END INIT INFO
  20.  
  21. # Don't use set -e; check exit status instead
  22.  
  23. # Exit silently if package is no longer installed
  24. [ -x /usr/sbin/alsactl ] || exit 0
  25.  
  26. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  27. MYNAME=/etc/init.d/alsa-utils
  28.  
  29. . /lib/lsb/init-functions
  30.  
  31. # $1 EXITSTATUS
  32. # [$2 MESSAGE]
  33. log_action_end_msg_and_exit()
  34. {
  35.     log_action_end_msg "$1" ${2:+"$2"}
  36.     exit $1
  37. }
  38.  
  39. # $1 PROGRAM
  40. executable()
  41. {
  42.     # If which is not available then we must be running before
  43.     # /usr is mounted on a system that has which in /usr/bin/.
  44.     # Conclude that $1 is not executable.
  45.     [ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
  46.     which "$1" >/dev/null 2>&1
  47. }
  48.  
  49. executable amixer || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }
  50.  
  51. bugout() { echo "${MYNAME}: Programming error" >&2 ; exit 123 ; }
  52.  
  53. # $1 <card ID> | "all"
  54. restore_levels()
  55. {
  56.     [ -f /var/lib/alsa/asound.state ] || return 1
  57.     CARD="$1"
  58.     [ "$1" = all ] && CARD=""
  59.     # Assume that if alsactl prints a message on stderr
  60.     # then it failed somehow.  This works around the fact
  61.     # that alsactl doesn't return nonzero status when it
  62.     # can't restore settings for the card
  63.     if MSG="$(alsactl restore $CARD 2>&1 >/dev/null)" && [ ! "$MSG" ] ; then
  64.         return 0
  65.     else
  66.         # Retry with the "force" option.  This restores more levels
  67.         # but it results in much longer error messages.
  68.         alsactl -F restore $CARD >/dev/null 2>&1
  69.         log_action_cont_msg "warning: 'alsactl restore${CARD:+ $CARD}' failed with error message '$MSG'"
  70.         return 1
  71.     fi
  72. }
  73.  
  74. # $1 <card ID> | "all"
  75. store_levels()
  76. {
  77.     CARD="$1"
  78.     [ "$1" = all ] && CARD=""
  79.     if MSG="$(alsactl store $CARD 2>&1)" ; then
  80.         sleep 1
  81.         return 0
  82.     else
  83.         log_action_cont_msg "warning: 'alsactl store${CARD:+ $CARD}' failed with error message '$MSG'"
  84.         return 1
  85.     fi
  86. }
  87.  
  88. echo_card_indices()
  89. {
  90.     if [ -f /proc/asound/cards ] ; then
  91.         sed -n -e's/^[[:space:]]*\([0-7]\)[[:space:]].*/\1/p' /proc/asound/cards
  92.     fi
  93. }
  94.  
  95. filter_amixer_output()
  96. {
  97.     sed \
  98.         -e '/Unable to find simple control/d' \
  99.         -e '/Unknown playback setup/d' \
  100.         -e '/^$/d'
  101. }
  102.  
  103. # The following functions try to set many controls.
  104. # No card has all the controls and so some of the attempts are bound to fail.
  105. # Because of this, the functions can't return useful status values.
  106.  
  107. # $1 <control>
  108. # $2 <level>
  109. # $CARDOPT
  110. unmute_and_set_level()
  111. {
  112.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  113.     amixer $CARDOPT -q set "$1" "$2" unmute 2>&1 | filter_amixer_output || :
  114.     return 0
  115. }
  116.  
  117. # $1 <control>
  118. # $CARDOPT
  119. mute_and_zero_level()
  120. {
  121.     { [ "$1" ] && [ "$CARDOPT" ] ; } || bugout
  122.     amixer $CARDOPT -q set "$1" "0%" mute 2>&1 | filter_amixer_output || :
  123.     return 0
  124. }
  125.  
  126. # $1 <control>
  127. # $2 "on" | "off"
  128. # $CARDOPT
  129. switch_control()
  130. {
  131.     { [ "$2" ] && [ "$CARDOPT" ] ; } || bugout
  132.     amixer $CARDOPT -q set "$1" "$2" 2>&1 | filter_amixer_output || :
  133.     return 0
  134. }
  135.  
  136. # $1 <card ID>
  137. sanify_levels_on_card()
  138. {
  139.     CARDOPT="-c $1"
  140.  
  141.     unmute_and_set_level "Master" "80%"
  142.     unmute_and_set_level "Master Mono" "80%"   # See Bug#406047
  143.     unmute_and_set_level "Master Digital" "80%"   # E.g., cs4237B
  144.     unmute_and_set_level "Playback" "80%"
  145.     unmute_and_set_level "Headphone" "70%"
  146.     unmute_and_set_level "PCM" "80%"
  147.     unmute_and_set_level "PCM,1" "80%"   # E.g., ess1969
  148.     unmute_and_set_level "DAC" "80%"     # E.g., envy24, cs46xx
  149.     unmute_and_set_level "DAC,0" "80%"   # E.g., envy24
  150.     unmute_and_set_level "DAC,1" "80%"   # E.g., envy24
  151.     unmute_and_set_level "Synth" "80%"
  152.     unmute_and_set_level "CD" "80%"
  153.  
  154.     mute_and_zero_level "Mic"
  155.     mute_and_zero_level "IEC958"         # Ubuntu #19648
  156.  
  157.     # Intel P4P800-MX  (Ubuntu bug #5813)
  158.     switch_control "Master Playback Switch" on
  159.     switch_control "Master Surround" on
  160.  
  161.     # Trident/YMFPCI/emu10k1:
  162.     unmute_and_set_level "Wave" "80%"
  163.     unmute_and_set_level "Music" "80%"
  164.     unmute_and_set_level "AC97" "80%"
  165.  
  166.     # DRC:
  167.     unmute_and_set_level "Dynamic Range Compression" "80%"
  168.  
  169.     # Required for HDA Intel (hda-intel):
  170.     unmute_and_set_level "Front" "80%"
  171.  
  172.     # Required for SB Live 7.1/24-bit (ca0106):
  173.     unmute_and_set_level "Analog Front" "80%"
  174.  
  175.     # Required at least for Via 823x hardware on DFI K8M800-MLVF Motherboard with kernels 2.6.10-3/4 (see ubuntu #7286):
  176.     switch_control "IEC958 Capture Monitor" off
  177.  
  178.     # Required for hardware allowing toggles for AC97 through IEC958,
  179.     #  valid values are 0, 1, 2, 3. Needs to be set to 0 for PCM1.
  180.     unmute_and_set_level "IEC958 Playback AC97-SPSA" "0"
  181.  
  182.     # Required for newer Via hardware (see Ubuntu #31784)
  183.     unmute_and_set_level "VIA DXS,0" "80%"
  184.     unmute_and_set_level "VIA DXS,1" "80%"
  185.     unmute_and_set_level "VIA DXS,2" "80%"
  186.     unmute_and_set_level "VIA DXS,3" "80%"
  187.  
  188.     # Required on some notebooks with ICH4:
  189.     switch_control "Headphone Jack Sense" off
  190.     switch_control "Line Jack Sense" off
  191.  
  192.     # Some machines need one or more of these to be on;
  193.     # others need one or more of these to be off:
  194.     #
  195.     # switch_control "External Amplifier" on
  196.     switch_control "Audigy Analog/Digital Output Jack" on
  197.     switch_control "SB Live Analog/Digital Output Jack" on
  198.     
  199.     # D1984 -- Thinkpad T61/X61
  200.     switch_control "Speaker" on
  201.     switch_control "Headphone" on
  202.  
  203.     # HDA-Intel w/ "Digital" capture mixer (See Ubuntu #193823)
  204.     unmute_and_set_level "Digital" "80%"
  205.  
  206.     # On MacBookPro5,3 and later models (See Bug#597791)
  207.     unmute_and_set_level "Front Speaker" "80%"
  208.        # On MacBook5,2 models (See Bug#602973)
  209.        unmute_and_set_level "LFE" "80%"
  210.  
  211.     # On Intel 82801H (See Bug#603550)
  212.     unmute_and_set_level "Speaker" "80%"
  213.  
  214.     return 0
  215. }
  216.  
  217. # $1 <card ID> | "all"
  218. sanify_levels()
  219. {
  220.     TTSDML_RETURNSTATUS=0
  221.     case "$1" in
  222.       all)
  223.         for CARD in $(echo_card_indices) ; do
  224.             sanify_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  225.         done
  226.         ;;
  227.       *)
  228.         sanify_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  229.         ;;
  230.     esac
  231.     return $TTSDML_RETURNSTATUS
  232. }
  233.  
  234. # $1 <card ID>
  235. preinit_levels_on_card()
  236. {
  237.     CARDOPT="-c $1"
  238.  
  239.     # Silly dance to activate internal speakers by default on PowerMac
  240.     # Snapper and Tumbler
  241.     id=`cat /proc/asound/card$1/id 2>/dev/null`
  242.     if [ "$id" = "Snapper" -o "$id" = "Tumbler" ]; then
  243.         switch_control "Auto Mute" off
  244.         switch_control "PC Speaker" off
  245.         switch_control "Auto Mute" on
  246.     fi
  247. }
  248.  
  249. # $1 <card ID> | "all"
  250. preinit_levels()
  251. {
  252.     TTSDML_RETURNSTATUS=0
  253.     case "$1" in
  254.       all)
  255.         for CARD in $(echo_card_indices) ; do
  256.             preinit_levels_on_card "$CARD" || TTSDML_RETURNSTATUS=1
  257.         done
  258.         ;;
  259.       *)
  260.         preinit_levels_on_card "$1" || TTSDML_RETURNSTATUS=1
  261.         ;;
  262.     esac
  263.     return $TTSDML_RETURNSTATUS
  264. }
  265.  
  266. # $1 <card ID>
  267. mute_and_zero_levels_on_card()
  268. {
  269.     CARDOPT="-c $1"
  270.     for CTL in \
  271.         Master \
  272.         PCM \
  273.         Synth \
  274.         CD \
  275.         Line \
  276.         Mic \
  277.         "PCM,1" \
  278.         Wave \
  279.         Music \
  280.         AC97 \
  281.         "Master Digital" \
  282.         DAC \
  283.         "DAC,0" \
  284.         "DAC,1" \
  285.         Headphone \
  286.         Speaker \
  287.         Playback
  288.     do
  289.         mute_and_zero_level "$CTL"
  290.     done
  291. #    for CTL in \
  292. #        "Audigy Analog/Digital Output Jack" \
  293. #        "SB Live Analog/Digital Output Jack"
  294. #    do
  295. #        switch_control "$CTL" off
  296. #    done
  297.     return 0
  298. }
  299.  
  300. # $1 <card ID> | "all"
  301. mute_and_zero_levels()
  302. {
  303.     TTZML_RETURNSTATUS=0
  304.     case "$1" in
  305.       all)
  306.         for CARD in $(echo_card_indices) ; do
  307.             mute_and_zero_levels_on_card "$CARD" || TTZML_RETURNSTATUS=1
  308.         done
  309.         ;;
  310.       *)
  311.         mute_and_zero_levels_on_card "$1" || TTZML_RETURNSTATUS=1
  312.         ;;
  313.     esac
  314.     return $TTZML_RETURNSTATUS
  315. }
  316.  
  317.  
  318. # $1 <card ID> | "all"
  319. card_OK()
  320. {
  321.     [ "$1" ] || bugout
  322.     if [ "$1" = all ] ; then
  323.         [ -d /proc/asound ]
  324.         return $?
  325.     else
  326.         [ -d "/proc/asound/card$1" ] || [ -d "/proc/asound/$1" ]
  327.         return $?
  328.     fi
  329. }
  330.  
  331. # If a card identifier is provided in $2 then regard it as an error
  332. # if that card is not present; otherwise don't regard it as an error.
  333.  
  334. case "$1" in
  335.   start)
  336.     EXITSTATUS=0
  337.     TARGET_CARD="$2"
  338.     case "$TARGET_CARD" in
  339.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Setting up ALSA" ;;
  340.       *) log_action_begin_msg "Setting up ALSA card ${TARGET_CARD}" ;;
  341.     esac
  342.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  343.     preinit_levels "$TARGET_CARD" || EXITSTATUS=1
  344.     if ! restore_levels "$TARGET_CARD" ; then
  345.         sanify_levels "$TARGET_CARD" || EXITSTATUS=1
  346.         restore_levels "$TARGET_CARD" >/dev/null 2>&1 || :
  347.     fi
  348.     log_action_end_msg_and_exit "$EXITSTATUS"
  349.     ;;
  350.   stop)
  351.     EXITSTATUS=0
  352.     TARGET_CARD="$2"
  353.     case "$TARGET_CARD" in
  354.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Shutting down ALSA" ;;
  355.       *) log_action_begin_msg "Shutting down ALSA card ${TARGET_CARD}" ;;
  356.     esac
  357.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  358.     store_levels "$TARGET_CARD" || EXITSTATUS=1
  359.     #mute_and_zero_levels "$TARGET_CARD" || EXITSTATUS=1
  360.     log_action_end_msg_and_exit "$EXITSTATUS"
  361.     ;;
  362.   restart|force-reload)
  363.     EXITSTATUS=0
  364.     $0 stop || EXITSTATUS=1
  365.     $0 start || EXITSTATUS=1
  366.     exit $EXITSTATUS
  367.     ;;
  368.   reset)
  369.     TARGET_CARD="$2"
  370.     case "$TARGET_CARD" in
  371.       ""|all) TARGET_CARD=all ; log_action_begin_msg "Resetting ALSA" ;;
  372.       *) log_action_begin_msg "Resetting ALSA card ${TARGET_CARD}" ;;
  373.     esac
  374.     card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
  375.     preinit_levels "$TARGET_CARD"
  376.     sanify_levels "$TARGET_CARD"
  377.     log_action_end_msg_and_exit "$?"
  378.     ;;
  379.   *)
  380.     echo "Usage: $MYNAME {start [CARD]|stop [CARD]|restart [CARD]|reset [CARD]}" >&2
  381.     exit 3
  382.     ;;
  383. esac
  384.  
  385.